package cn.ffcs.bpmpvc.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.views.ProcessView;
import cn.ffcs.bpmpvc.web.views.TaskView;
import cn.ffcs.businessrequest.model.ProdAdvReq;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;

public class FlowPvcTestAction <D extends TransactProcessStatus> extends BaseAction<D>
{

    /** TODO 请输入字段日志 */
    private static final long serialVersionUID = 1L;
    private String processId;
    private String taskId;
    private ProcessPvcService processPvcService;
    private TaskService taskService;
    private ProdAdvReq prodAdvReq;
    public ProdAdvReq getProdAdvReq() {
		return prodAdvReq;
	}

	public void setProdAdvReq(ProdAdvReq prodAdvReq) {
		this.prodAdvReq = prodAdvReq;
	}

	public String applyInit(){
        ProcessView processView=getProcessPvcService().getInitProcessInfo(processId);
        ApplicationContextUtil.getHttpServletRequest().setAttribute("processView", processView);
        return SUCCESS;
    }

    public String startFlow(){
        String isCommit=ApplicationContextUtil.getContextObject("isCommit");
        User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
        HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
        List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
        getProcessPvcService().startProcessInst(processId,user.getPerson(),nextTasksList,Boolean.valueOf(isCommit));
        return null;
        //return SUCCESS;
    }
    
    public String index(){
        User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
        getProcessPvcService().startProcessInst(processId,user.getPerson());
        return null;
        //return SUCCESS;
    }
    
    public String showTask(){
        taskId= ApplicationContextUtil.getHttpServletRequest().getParameter("flowTaskId");
        TaskView taskView=getTaskService().getTaskView(taskId);
        ApplicationContextUtil.getHttpServletRequest().setAttribute("taskView", taskView);
        return SUCCESS;
    }
    
    public String rollBackTask(){
        String content=ApplicationContextUtil.getContextObject("commentComtent");
        taskId= ApplicationContextUtil.getHttpServletRequest().getParameter("flowTaskId");
        getTaskService().rollBackTask(taskId, content);
        returnMsg="回退成功";
        return SUCCESS;
    }
    /**
     * 回收
     * TODO 请输入方法说明
     * @return
     */
    public String recyclingTask(){
        taskId= ApplicationContextUtil.getHttpServletRequest().getParameter("flowTaskId");
        User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
        getTaskService().recyclingTask(taskId, String.valueOf(user.getPerson().getId()), "被"+user.getPerson().getName()+"回收");
        returnMsg="回收成功";
        return SUCCESS;
    }

    public String saveTask(){
        HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
        taskId= req.getParameter("taskId");
        List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
        String content=req.getParameter("commentComtent");
        getTaskService().saveTaskAssignUser(taskId, content, nextTasksList);
        return null;
        //return SUCCESS;
    }
    public String commitTask(){
        HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
        taskId= req.getParameter("flowTaskId");
        List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
        String content=req.getParameter("commentComtent");
        getTaskService().completeTask(taskId, content, nextTasksList);
        //return null;
        return SUCCESS;
    }
    public String commitTask2(){
        HttpServletRequest request=ApplicationContextUtil.getHttpServletRequest();
        taskId= request.getParameter("flowTaskId");
        List<TaskAssignBean> nextTasksList= getNextTaskUserList(request);
        String content =request.getParameter("commentComtent");
        getTaskService().completeTask(taskId, content, nextTasksList);
        return null;
    }
    private List<TaskAssignBean> getNextTaskUserList(HttpServletRequest req)
    {
        List<TaskAssignBean> nextTasksList=new ArrayList<TaskAssignBean>();
        String taskNodeId[]=req.getParameterValues("taskNodeId");
        String userId[]=req.getParameterValues("userId");
        if(taskNodeId!=null&&taskNodeId.length>0){
            for (int i = 0; i < taskNodeId.length; i++)
            {
                TaskAssignBean assignBean=new TaskAssignBean(); 
                assignBean.setTaskNodeId(taskNodeId[i]);
                assignBean.setUserId(userId[i]);
                nextTasksList.add(assignBean);
            }
        }
        return nextTasksList;
    }
    
    public String getProcessId()
    {
        return processId;
    }
    public void setProcessId(String processId)
    {
        this.processId = processId;
    }
    public ProcessPvcService getProcessPvcService()
    {
        return processPvcService;
    }
    public void setProcessPvcService(ProcessPvcService processPvcService)
    {
        this.processPvcService = processPvcService;
    }
    public TaskService getTaskService()
    {
        return taskService;
    }
    public void setTaskService(TaskService taskService)
    {
        this.taskService = taskService;
    }
    
    
}
